package com.lxd.sunso.product.components;

import org.apache.commons.lang.StringUtils;

import java.math.BigInteger;

public class ConversionUtil {
    //16进制转换为布尔值
    public static String HexToBoolean(String hexStr, String sort) {
        if (sort.equals("1")) {
            hexStr = changeSort(hexStr);
        }
        int i = Integer.parseInt(hexStr, 16);
        return String.valueOf(i);
    }


    //16位无符号数（16进制）转十进制
    public static String unsignedHexToDec(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        int i = Integer.parseInt(str, 16);
        String s = String.valueOf(i);
        return s;
    }

    public static String unsignedHexToDec(String str) {
        int i = Integer.parseInt(str, 16);
        String s = String.valueOf(i);
        return s;
    }

    //16位无符号数(16进制)转十进制（/10）
    public static String unsignedHexToDec10(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        int ia = Integer.parseInt(str, 16);
        float i = (float) ia / 10;
        String s = String.valueOf(i);
        return s;
    }

    //16位无符号数(16进制)转十进制（/100）
    public static String unsignedHexToDec100(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        int ia = Integer.parseInt(str, 16);
        float i = (float) ia / 100;
        String s = String.valueOf(i);
        return s;
    }

    //16位有符号数(16进制)转十进制
    public static String signedHexToDec(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        String s = String.valueOf((Integer.valueOf(str, 16).shortValue()));
        return s;
    }

    //16位有符号数（16进制）转十进制（/10）
    public static String signedHexToDec10(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        String s = String.valueOf((Integer.valueOf(str, 16).shortValue()));
        Float value = Float.valueOf(s);
        value /= 10;
        return String.valueOf(value);
    }

    //16位有符号数（16进制）转十进制（/100）
    public static String signedHexToDec100(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        String s = String.valueOf((Integer.valueOf(str, 16).shortValue()));
        Float value = Float.valueOf(s);
        value /= 100;
        return String.valueOf(value);
    }

    //32位无符号数
    public static String unsignedHex32ToDec(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        long value = Long.parseLong(str, 16);
        return String.valueOf(value);
    }

    //32位有符号数
    public static String signedHex32ToDec(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        BigInteger bi = new BigInteger(str, 16);
        return String.valueOf(bi.intValue());
    }

    //16进制转单精度浮点数
    public static String hexToFloat(String str, String sort) {
        if (sort.equals("1")) {
            str = changeSort(str);
        }
        return String.valueOf(Float.intBitsToFloat(new BigInteger(str, 16).intValue()));
    }

    //16进制转字符串
    public static String hexToString(String hexStr, String sort) {
        if (sort.equals("1")) {
            hexStr = changeSort(hexStr);
        }
        String str = "0123456789abcdef";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        String s = new String(bytes);
        return s;
    }

    public static String hexToString(String hexStr) {
        String str = "0123456789abcdef";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        String s = new String(bytes);
        return s;
    }

    //布尔值转16进制
    public static String booleanToHex(String str, String sort) {
        String hexString = Integer.toHexString(Integer.valueOf(str));
        if (sort.equals("1")) {
            String s = changeSort(StringUtils.leftPad(hexString, 4, '0'));
            return s;
        } else {
            return StringUtils.leftPad(hexString, 4, '0');
        }
    }

    //十进制16位无符号转16进制
    public static String unsignedDecToHex(String str, String sort) {
        String hexString = Integer.toHexString(Integer.valueOf(str));
        if (sort.equals("1")) {
            String s = changeSort(StringUtils.leftPad(hexString, 4, '0'));
            return s;
        } else {
            return StringUtils.leftPad(hexString, 4, '0');
        }
    }

    //十进制16位无符号转16进制*10
    public static String unsignedDecToHex10(String str, String sort) {
        Float value = Float.valueOf(str);
        value *= 10;
        float f = value;
        int data = (int) f;
        String hexString = Integer.toHexString(data);
        if (sort.equals("1")) {
            String s = changeSort(StringUtils.leftPad(hexString, 4, '0'));
            return s;
        } else {
            return StringUtils.leftPad(hexString, 4, '0');
        }
    }

    //十进制16位无符号转16进制*100
    public static String unsignedDecToHex100(String str, String sort) {
        Float value = Float.valueOf(str);
        value *= 100;
        float f = value;
        int data = (int) f;
        String hexString = Integer.toHexString(data);
        if (sort.equals("1")) {
            String s = changeSort(StringUtils.leftPad(hexString, 4, '0'));
            return s;
        } else {
            return StringUtils.leftPad(hexString, 4, '0');
        }
    }

    //十进制16位有符号转16进制(如果为符号的时候，是会超过4位的)
    public static String signedDecToHex(String str, String sort) {
        Long aLong = Long.valueOf(str);
        String hexString = Long.toHexString(aLong);
        String data = null;
        if (hexString.length() > 4) {
            data = hexString.substring(hexString.length() - 4, hexString.length());
        } else {
            data = StringUtils.leftPad(hexString, 4, '0');
        }

        if (sort.equals("1"))
            return changeSort(data);
        else
            return data;
    }

    //十进制16位有符号转16进制10
    public static String signedDecToHex10(String str, String sort) {
        Float value = Float.valueOf(str);
        value *= 10;
        float f = value;
        long data = (long) f;
        String hexString = Long.toHexString(data);
        String s = null;
        if (hexString.length() > 4) {
            s = hexString.substring(hexString.length() - 4, hexString.length());
        } else {
            s = StringUtils.leftPad(hexString, 4, '0');
        }

        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;
    }

    //十进制16位有符号转16进制100
    public static String signedDecToHex100(String str, String sort) {
        Float value = Float.valueOf(str);
        value *= 100;
        float f = value;
        long data = (long) f;
        String hexString = Long.toHexString(data);
        String s = null;
        if (hexString.length() > 4) {
            s = hexString.substring(hexString.length() - 4, hexString.length());
        } else {
            s = StringUtils.leftPad(hexString, 4, '0');
        }
        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;
    }

    //32位无符号十进制转16进制
    public static String unsignedDecToHex32(String str, String sort) {
        Long aLong = Long.valueOf(str);
        String s1 = Long.toHexString(aLong);
        String s = StringUtils.leftPad(s1, 8, '0');
        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;
    }

    //32位有符号十进制转16进制
    public static String signedDecToHex32(String str, String sort) {
        Long aLong = Long.valueOf(str);
        String hexString = Long.toHexString(aLong);
        String s = null;
        if (hexString.length() > 8) {
            s = hexString.substring(hexString.length() - 8, hexString.length());
        } else {
            s = StringUtils.leftPad(hexString, 8, '0');
        }

        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;

    }

    //单精度浮点数转16进制
    public static String floatToHex(String str, String sort) {
        Float value = Float.valueOf(str);
        String s = Integer.toHexString(Float.floatToIntBits(value));
        if (s.length() < 8) {
            s = StringUtils.leftPad(s, 8, '0');
        }
        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;
    }

    //字符串转16进制
    public static String StringToHex(String hexStr, String sort) {
        char[] chars = "0123456789abcdef".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = hexStr.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        String s = sb.toString().trim();

        if (s.length() < 8) {
            s = StringUtils.leftPad(s, 8, '0');
        }
        if (sort.equals("1"))
            return changeSort(s);
        else
            return s;

    }

    public static String StringToHex(String hexStr) {
        char[] chars = "0123456789abcdef".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = hexStr.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    //十进制转16进制(不增长为4位)
    public static String DecToHex(int num) {
        String hexString = Integer.toHexString(num);
        String s = StringUtils.leftPad(hexString, 2, '0');
        return s;
    }

    //16进制编码转为名称
    public static String HexToName(String data) {
        data = data.substring(12, 28);
        StringBuffer buffer = new StringBuffer();
        int start = 0;
        int end = 0;
        while (start < data.length()) {
            end = start + 2;
            String substring = data.substring(start, end);
            char letter = (char) Integer.parseInt(substring, 16); //
            buffer.append(letter);
            start = end;
        }
        return buffer.toString();
    }

    //低位在前的转换问题
    private static String changeSort(String hexStr) {
        int length = hexStr.length();
        int n = 0;
        StringBuilder builder = new StringBuilder();
        while (n < length) {
            builder.append(hexStr.substring(n + 2, n + 4));
            builder.append(hexStr.substring(n, n + 2));
            n += 4;
        }
        String string = builder.toString();
        return string;
    }

    //字符串转换为字节
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    //远程升级的十进制转16进制（结果为一个字节）
    public static String DecToHexOneByte(int by) {
        String hexString = Integer.toHexString(by);
        if (hexString.length() > 2) {
            return hexString.substring(hexString.length() - 2, hexString.length());
        }
        return StringUtils.leftPad(hexString, 2, "0");
    }
}
